/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.basedata.config;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;

/**
 * 解决：RestTemplate自带的HttpMessageConverters中没用能处理
 * content-type=application/javascript的response的HttpMessageConverter
 * 
 * @author icanci
 * @date Created in 2021/04/27 20:06
 * @version 1.0
 */
//@Configuration
@Deprecated
public class MyApplicationJavascriptHttpMessageConverter extends AbstractHttpMessageConverter {
    /**
     * restTemplate获得的响应的content-type满足下面的条件
     * 才会被该HttpMessageConverter处理
     */
    public MyApplicationJavascriptHttpMessageConverter() {
        // application/javascript
        super(new MediaType("application", "javascript", Charset.forName("UTF-8")));
    }

    /**
     * 检查传入的字节码是否符合要求
     *
     * @param clazz
     * @return 返回true才会执行readInternal()
     */
    @Override
    protected boolean supports(Class clazz) {
        return true;
    }

    /**
     * 将restTemplate获得的响应转化成clazz对应的类型
     *
     * @param clazz
     * @param inputMessage restTemplate获得的响应
     * @return
     */
    @Override
    protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        //restTemplate发送请求后获得的响应
        InputStream body = inputMessage.getBody();

        //将响应转成对应类型
        // 就是restTemplate.getForObject(url, Map.class)的返回值
        return JSON.parseObject(body, clazz);
    }

    /**
     * 还不知道该方法的功能
     * 该方法也不会被执行到
     *
     * @param o
     * @param outputMessage
     * @throws IOException
     * @throws HttpMessageNotWritableException
     */
    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        //do nothing
        System.out.println("do nothing");
    }
}
